package com.cakeclient.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakeclient.database.DataSource;
import com.cakeclient.database.IDatabasable;
import com.cakeclient.database.ProductTable;
import com.cakeclient.database.RecommendedPriceTable;

import java.util.ArrayList;
import java.util.List;

public class RecommendedPrice implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private Float value;
    public DeliveryType deliveryType;

    private RecommendedPrice(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(RecommendedPriceTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(ProductTable.REMOTE_ID)));
        value = cursor.getFloat(cursor.getColumnIndexOrThrow(RecommendedPriceTable.VALUE));
        deliveryType = DeliveryType.getOne(dataSource, cursor.getInt(cursor.getColumnIndexOrThrow(RecommendedPriceTable.DELIVERYTYPE)));

    }

    public RecommendedPrice(Float value, DeliveryType deliveryType) {
        this.value = value;
        this.deliveryType = deliveryType;
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public Long getIdDB() {
        return idDB;
    }

    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        RecommendedPrice bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = RecommendedPrice.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, RecommendedPriceTable.getInstance());
        } else {
            dataSource.update(this, RecommendedPriceTable.getInstance());
        }
        {
            //Aucune Liste !   C'est trop bien !
        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, RecommendedPriceTable.getInstance());
            dataSource.close();
        }
    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(RecommendedPriceTable.REMOTE_ID, this.getRemoteID());
        values.put(RecommendedPriceTable.VALUE, this.value);
        values.put(RecommendedPriceTable.DELIVERYTYPE, this.getDeliveryType() != null ? this.getDeliveryType().getIdDB() : null);
        return values;
    }

    public static RecommendedPrice getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, RecommendedPriceTable.getInstance());
        cursor.moveToFirst();
        RecommendedPrice item = null;
        while (!cursor.isAfterLast()) {
            item = new RecommendedPrice(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static RecommendedPrice getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, RecommendedPriceTable.getInstance());
        cursor.moveToFirst();
        RecommendedPrice item = null;
        while (!cursor.isAfterLast()) {
            item = new RecommendedPrice(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<RecommendedPrice> getAll(Context context) {
        List<RecommendedPrice> result = new ArrayList<RecommendedPrice>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(RecommendedPriceTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new RecommendedPrice(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public Float getValue() {
        return value;
    }

    public void setValue(Float value) {
        this.value = value;
    }

    public DeliveryType getDeliveryType() {
        return deliveryType;
    }

    public void setDeliveryType(DeliveryType deliveryType) {
        this.deliveryType = deliveryType;
    }

    public static RecommendedPrice getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, RecommendedPriceTable.getInstance());
        cursor.moveToFirst();
        RecommendedPrice item = null;
        while (!cursor.isAfterLast()) {
            item = new RecommendedPrice(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }
}
